import pandas as pd
import os


def splitExcelByLineNums(excelPath,lineNums,basepath):
    df = pd.read_excel(excelPath,dtype=str)


    #window默认的桌面桌面路径
    desktop=os.path.join(os.path.expanduser("~"), 'Desktop')

    if (basepath==''):
        basepath = desktop


    #不带后缀文件名
    filename = os.path.basename(excelPath).split(".")[0]
    # print(filename)

    #文件类型：.xls
    filetype = os.path.splitext(excelPath)[-1]


    index  = 0
    for i in range(0,df.shape[0]+1,lineNums):
        print(i)
        tempdf = df[i:i+lineNums]
        index = index+1

        writer=pd.ExcelWriter(os.path.join(basepath, filename+str(index)+filetype))
        # index=True会多出一列
        tempdf.to_excel(writer, index=False)
        writer.save()
        print("分割后的文件会放到目录："+basepath)

    #     # print(tempdf)





if __name__ == '__main__':

    filepath ="../数据测试集/分割.xls"
    nums = 5
    basepath = "数据结果集"

    splitExcelByLineNums(filepath,nums,basepath)
